import './widgets'
import { setup as setupConfig } from './config'
import { setup as setupApp } from './app'
import { setup as setupMap } from './map'
import { setup as setupMapItem } from './map-item'
import { setup as setupMapInteraction } from './map-interaction'
import { setup as setupAuxGraphic } from './aux-graphic'
import { setup as setupModel } from './model'
import { setup as setupAtmosphere } from './atmosphere'
import { setup as setupBADA } from './bada'
import { setup as setupRemote } from './remote'
import { setup as setupStats } from './stats'
import { setup as setupWidgetsExample } from './widgets-example'
import { setup as setupDemo } from './demo'
import { modal, type LoadingScreenInstance } from './widgets'
import { closeWindow } from './common'

async function doSetup(ls: LoadingScreenInstance) {
  await setupConfig()
  ls.updateMessage('正在创建app')
  await setupApp()
  ls.updateMessage('正在创建地图')
  await setupMap()
  await setupMapItem()
  await setupMapInteraction()
  ls.updateMessage('正在加载数据模型')
  await setupModel(ls)
  ls.updateMessage('正在创建辅助图形功能')
  await setupAuxGraphic()
  ls.updateMessage('正在创建大气模块')
  await setupAtmosphere()
  ls.updateMessage('正在加载BADA模块')
  await setupBADA()
  await setupStats()
  await setupWidgetsExample()
  await setupDemo()
  await setupRemote()
  ls.destroy()
}

async function setup() {
  const ls = modal.loadingScreen({ message: '正在加载...' })
  try {
    await doSetup(ls)
    ls.destroy()
  } catch (ex) {
    console.error(ex)
    ls.destroy()
    const ok = await modal.confirm({
      title: '页面加载失败',
      content: '页面加载失败, 是否重新加载页面',
      positiveText: '确定',
      negativeText: '关闭页面'
    })
    if (ok) {
      window.location.reload()
    } else {
      closeWindow()
    }
  }
}

setup()
